﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;

namespace NumericalMethod
{
    public partial class ZedGraph : UserControl
    {
        public ZedGraph()
        {
            InitializeComponent();
            m_graphPane = zg1.GraphPane;
        }
        public ZedGraph(string _plotTitle, double[,] _data)
        {
            InitializeComponent();
            m_graphPane = zg1.GraphPane;
            this.PlotTitle = _plotTitle;
            this.Datas = _data;
        }
        private GraphPane m_graphPane;
        private PointPairList m_pointsList = null;
        public string PlotTitle { get; set; }
        public string XAxisTitle { get; set; }
        public string YAxisTitle { get; set; }
        public string XAxisUnit { get; set; }
        public string YAxisUnit { get; set; }
        private double[,] _datas = null;
        public double[,] Datas
        {
            get { return _datas; }
            set { value = _datas; }
        }

        private void ProcessPointsData()
        {
            for (int i = 0; i < _datas.Length; i++)
            {
                m_pointsList.Add(_datas[i, 0], _datas[i, 1]);
            }
        }

        private void CreateLineGraph()
        {


            //clear if anything exists.            
            m_graphPane.CurveList.Clear();

            string _graphTitle = "", _xTitle = "", _yTitle = "";

            // Set the titles and axis labels
            SetLineBarTitleAndAxisDetails(ref _graphTitle, ref _xTitle, ref _yTitle);
            m_graphPane.Title.Text = _graphTitle;
            m_graphPane.XAxis.Title.Text = _xTitle;
            m_graphPane.YAxis.Title.Text = _yTitle;

            // Generate a blue curve with Star symbols
            LineItem myCurve = m_graphPane.AddCurve(PlotTitle, m_pointsList, Color.Blue, SymbolType.Star);

            // Calculate the Axis Scale Ranges
            zg1.AxisChange();
        }
        private void SetLineBarTitleAndAxisDetails(ref string _graphTitle, ref string _xTitle, ref string _yTitle)
        {
            _graphTitle = PlotTitle;
            string _xTitleTemp = XAxisTitle;
            string _yTitleTemp = YAxisTitle;
            string _xUnit = XAxisUnit;
            string _yUnit = YAxisUnit;

            if (_graphTitle == "")
            {
                _graphTitle = "Graph Test";
            }

            if (_xTitleTemp == "")
            {
                _xTitle = "X Axis";
            }

            if (_yTitleTemp == "")
            {
                _yTitle = "Y Axis";
            }

            if (_xUnit != "")
            {
                _xTitle = _xTitleTemp + " (" + _xUnit + " )";
            }

            if (_yUnit != "")
            {
                _yTitle = _yTitleTemp + " (" + _yUnit + " )";
            }
        }
        private void SetSize()
        {
            zg1.Location = new Point(5, 168);
            // Leave a small margin around the outside of the control
            zg1.Size = new Size(this.ClientRectangle.Width - 10, this.ClientRectangle.Height - 168);
        }

        public void PlotLineGraph()
        {
            ProcessPointsData();
            CreateLineGraph();
            SetSize();
        }
    }
}
